//开始构建辅助函数，用来完成state和组件props的绑定，同时完成dispatch和组件props的绑定 
import {connect} from "react-redux";
//导入stores
import stores from "./store"; 

//将state状态值和组件的props属性绑定
const mapState = function(Com, props){
    const mapStateToProps = (state)=>{
        const obj = {}; //存储需要绑定的属性
        for(let index in props){
            obj[props[index]] = state[props[index]];
        }
        return obj;
    }

    return connect(mapStateToProps)(Com);
} 

//定义dispatch和props属性进行数据绑定
const mapDispatch = function(Com, props){
   const mapDispatchToProps = (dispatch)=>{
        const obj = {}; // 用来存储需要执行的函数操作
        for(let index in props){
            obj[props[index]] = (params=null)=>{
                const actionFun = stores.actions[props[index]];
                if(params){
                    actionFun(dispatch, params);
                }else{
                    actionFun(dispatch);
                }
            }
        }
        return obj;
   }

   return connect(null, mapDispatchToProps)(Com);
} 

export {mapState, mapDispatch};